package com.yanqu.road.entity.activity.springdinner.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

public class SpringDinnerConfig {

    private RandomHelper randomHelper = new RandomHelper();

    //最大的桌子数，大于这个就移除
    private int maxTableCount = 500;

    private int activityId;
    private ActivityInfo activityInfo;

    private List<Long> openTableScoreList;

    private List<Integer> redPacketParamList;

    private Integer SPRING_DINNER_ROBOT_USER_COUNT = 60;

    private String SPRING_DINNER_OPEN_LIMIT = "500|1000|2000|3000|4000|5000|6000|7000|8000|9000|10000|11000|12000|13000|14000|15000|16000|17000|18000|19000|20000|21000|22000|23000|24000|25000|26000|27000|28000|29000|30000|31000|32000|33000|34000|35000|36000|37000|38000|39000|40000";

    //红包参数（发红包上限数量|抢红包上限数量|发红包文本字符|抢红包文本字符|单个红包元宝数|单个红包人数|单个红包保底元宝数）
    private String SPRING_DINNER_RED_BAG_PARAM = "5|1|12|12|50|5|1|500|1500";

    public void initMaxTableCount(int count){
        if(count > 0){
            maxTableCount = count;
        }
    }

    public int getMaxTableCount(){
        return maxTableCount;
    }

    public void initOpenLimit(String str){
        if(!"".equals(str)){
            SPRING_DINNER_OPEN_LIMIT = str;
        }
        openTableScoreList = StringUtils.stringToLongList(SPRING_DINNER_OPEN_LIMIT,"\\|");
    }

    public void initRobotUserCount(int userCount){
        if(userCount > 0){
            SPRING_DINNER_ROBOT_USER_COUNT = userCount;
        }
    }

    public void initRedPacket(String str){
        if(!"".equals(str)){
            SPRING_DINNER_RED_BAG_PARAM = str;
        }
        redPacketParamList = StringUtils.stringToIntegerList(SPRING_DINNER_RED_BAG_PARAM,"\\|");
    }

    //当前积分，要开第几场
    public long getOpenLimitScore(int count){
        if(count - 1 >= 0 && count - 1 < openTableScoreList.size()){
            return openTableScoreList.get(count - 1);
        }else{
            return Long.MAX_VALUE;
        }
    }

    public int getLimitOpenSameTime(){
        return redPacketParamList.get(0);
    }

    public int getLimitRobTimes(){
        return redPacketParamList.get(1);
    }

    public int getLimitOpenNoteLength(){
        return redPacketParamList.get(2);
    }

    public int getLimitRobNoteLength(){
        return redPacketParamList.get(3);
    }

    public int getRedPacketAllValue(){
        return redPacketParamList.get(4);
    }

    public int getRedPacketUserCount(){
        return redPacketParamList.get(5);
    }

    public int getRedPacketRobMinValue(){
        return redPacketParamList.get(6);
    }


    public int getRedPacketRobMinParam(){
        return redPacketParamList.get(7);
    }

    public int getRedPacketRobMaxParam(){
        return redPacketParamList.get(8);
    }
    //随机红包
    public int randomRedPacketValue(int remainValue,int robUserCount){
        int remainUserCount = getRedPacketUserCount() - robUserCount;
        int randomValue = remainValue - remainUserCount * getRedPacketRobMinValue();
        //最后一个人直接全给
        if(getRedPacketUserCount() - robUserCount == 1){
            return remainValue;
        }
        //配错错误了，保底都不够发，直接发保底
        if(randomValue < getRedPacketRobMinValue()){
            return getRedPacketRobMinValue();
        }
        return randomHelper.next(getRedPacketRobMinValue(),randomValue + 1);
    }

    //随机一个玩家没抢过的桌子
    public int randomOneTable(int min,int max){
        return randomHelper.next(min,max);
    }

    //改了
    //剩余随机总值=参数1-参数2*参数3-∑随机值
    //随机值=RANDBETWEEN（剩余随机总值/剩余随机人数*参数4，剩余随机总值/剩余随机人数*参数5）
    //红包金额=保底值+随机值
    //最后一个红包金额=保底值+剩余随机总值
    public int randomRedPacketValueNew(int remainValue, Map<Long, RobRedPacketUser> robUserMap){
        //配错错误了，保底都不够发，直接发保底
        if(remainValue < getRedPacketRobMinValue()){
            return getRedPacketRobMinValue();
        }
        //最后一个人直接全给
        if(getRedPacketUserCount() - robUserMap.size() == 1){
            return remainValue;
        }
        int randomRemainValue = getRedPacketAllValue() - getRedPacketRobMinValue() * getRedPacketUserCount();
        for(RobRedPacketUser robUser : robUserMap.values()){
            randomRemainValue = randomRemainValue - (robUser.getValue() - getRedPacketRobMinValue());
        }
        int remainUserCount = getRedPacketUserCount() - robUserMap.size();
        int randomMinValue = (randomRemainValue * getRedPacketRobMinParam())/(remainUserCount * 1000);
        int randomMaxValue = (randomRemainValue * getRedPacketRobMaxParam())/(remainUserCount * 1000);
        return randomHelper.next(randomMinValue,randomMaxValue + 1) + getRedPacketRobMinValue();
    }


    public int getRobotUserCount(){
        return SPRING_DINNER_ROBOT_USER_COUNT;
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }
}
